<?php
/**
* Django's templating engine (partial) PHP implementation.
* provides some template tags support
* @link http://djangoproject.com
* this file is from a webappkit package (kit)
* @link http://webappkit.net
* @package djtpl
* @author J.Ducastel <jeremie@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* tag node : ifisin
* <code>{% ifisin somevar somecollection %} somevar is in somecollection{% endifisin %}</code>
*/
class djtplTagNode_ifInCollection extends djtplTagNode {

	/**
	* builds itself from tokens
	* @param djangoTplToken &$selfToken
	* @param array &$nextTokens djangoTplToken
	* @access public
	* @return bool
	*/
	function buildFromTokens(&$selfToken, &$nextTokens) {
		// importing def
		$this->def=$selfToken->tagdef; // echo $this->def;
		$this->buildChildNodesFromTokens($nextTokens);
		return true;
	}/**/

	/**
	* is the node atomic (has no children)
	* @access public
	* @return bool
	*/
	function isAtomic() {
		return false;
	}/**/
	
	/**
	* renders
	* @param djangoTplContext
	* @access public
	* @return string
	*/
	function render(&$context) {
		// parsing def
		$words=explode(' ',$this->def);
		$item=djtplParser::evalExpression($context,$words[0]);
		$collection=djtplParser::evalExpression($context,$words[1]);
		// echo '<pre>item = '; print_r($item); echo "\ncolelction = "; print_r($collection);
		if (is_array($collection)) {
			// looping array
			foreach (array_keys($collection) as $key) {
				// echo '<pre>item = '; print_r($item); echo "\ncollection item = "; print_r($collection[$key]);
				//if (serialize($collection[$key])==serialize($item)) {
				if ($collection[$key]==$item) {
					// echo '<p>match !</p>';
					return $this->renderChildNodes($context);
				}
			}
			// not in collection
			return '';
		} else
			return '';
	}/**/
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	/**
	* @var type desc
	*/
	var $tagname='ifincollection';
}/* end of djtplTagNode_if class */
?>